<template>
	<view>
		
		<!-- #ifdef APP-PLUS || MP-WEIXIN -->
		<u-navbar title="绑定的微信" back-icon-color="#000" :is-back="true" title-color="#000" :background="{ background:'#ffffff'}" :border-bottom="true"></u-navbar>
		<!-- #endif -->

		<view class="hb_page_wrap">
			<image class="portrait" :src="BestImgUrl + 'wx_icon.png'"></image>
			<view class="portrait_name">请点击按钮完成微信绑定</view>
			<view class="lijipay">
				
				<!-- #ifdef APP-PLUS -->
				<u-button hover-class="none" type="info" shape="circle" :ripple="false" :loading="Payloading" @click="wxAppLogon()">绑定微信</u-button>
				<!-- #endif -->
				
				<!-- #ifdef MP-WEIXIN -->
				<u-button hover-class="none" type="info" shape="circle" :ripple="false" :loading="Payloading" @click="wxminiAppLogon()">绑定微信</u-button>
				<!-- #endif -->
		
			</view>
		</view>

	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				BestImgUrl: app.globalData.imgurlBwc,
				Payloading: false,
				userData:{}
			}
		},
		onShow() {
			let that = this
			let userInfo = uni.getStorageSync('userinfo')
			that.userData = userInfo
		},
		methods: {
			wxAppLogon() {
				//微信APP登录方式
				let that = this
			
				that.Payloading = true
				setTimeout(() => {
					that.Payloading = false
					uni.login({
						"provider": "weixin",
						"onlyAuthorize": true, // 微信登录仅请求授权认证
						success: function(loginRes) {
							//console.log('调起微信APP信息', loginRes);
							
							//使用code换取openid和unionid
							that.$api.wxappLogon({
								code: loginRes.code
							}).then(res => {
								//console.log('微信APP登录', res)
								uni.setStorageSync('token', res.data.result.token)
								uni.switchTab({
									url: '/pages/WoDe/WoDe'
								})
							}).catch(err => {
								uni.showToast({
									title: err.data.msg,
									icon: 'none',
									duration: 2000
								});
							})
							
						},
						fail: (err) => {
							//console.log(err)
						}
					})
				}, 2000)
				
			},
			
			wxminiAppLogon() {
				let that = this
				
				that.Payloading = true
				
				uni.login({
					provider: 'weixin',
					success: function(login_res) {
						console.log('获取小程序code', login_res)
				
						that.$api.wxminiappLogon({
							code: login_res.code, //	是	string	登录授权code
							avatar: that.$store.state.baseUrl + '/public/uploads/h5/me/Group-33475.png', //	是	string	用户头像
							nickname: '领食惠探店用户', //	是	string	用户昵称
							appid: uni.getStorageSync('miniProgramAppId')
						}).then(res => {
							that.Payloading = false
							console.log('微信小程序登录', res)
							uni.setStorageSync('token', res.data.result.token)
							uni.switchTab({
								url: '/pages/WoDe/WoDe'
							})
						}).catch(err => {
							that.Payloading = false
							uni.showToast({
								title: err.data.msg,
								icon: 'none',
								duration: 2000
							});
						})
				
					},fail:function(err){
						that.Payloading = false
					}
				});
				
			},
			
			
			
			
			
			
			
			
			
			
			
			
		}
	}
</script>

<style lang="scss">
	.hb_page_wrap {
		text-align: center;
		padding: 140rpx 40rpx 40rpx 40rpx;
		.portrait {
			width: 140rpx;
			height: 140rpx;
			border-radius: 200rpx;
		}
		.portrait_name {
			text-align: center;
			font-size: 30rpx;
			margin-top: 20rpx;
			color: #999;
		}
	}
	.lijipay {
		width: 100%;
		margin-top: 100rpx;
		position: relative;

		.u-size-default {
			height: 80rpx !important;
			line-height: 80rpx !important;
			background-image: linear-gradient(to right,  #FE9400, #FF3D22) !important;
			color: #fff;
		}
	}
</style>
